//app.js
const valid = require('/util/valid.js');
App({
  onLaunch: function () {
    
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // env 参数说明：
        //   env 参数决定接下来小程序发起的云开发调用（wx.cloud.xxx）会默认请求到哪个云环境的资源
        //   此处请填入环境 ID, 环境 ID 可打开云控制台查看
        //   如不填则使用默认环境（第一个创建的环境）
        // wxchat-dev
        env: 'wxchat-dev-x9y0d',
        traceUser: true,
      })
    }
    
    this.globalData = {}
  },
  upOrcInvoiceImage: function (res, index, ocrType, isIoscamera, goTypeM) {//上传识别发票
    //isIoscamera 是否是ios 相机拍摄
    //isWxAli 是否是微信，支付宝账单识别
    //goType 默认redirectTo，传入其他goTypeM
    console.log('上传orc', res)

    var that = this;
    var size = 0;
    var imgArr = []
    var tempFilePath;
    if (res.tempImagePath) {//自定义拍照
      tempFilePath = res.tempImagePath
      imgArr.push(res.tempImagePath)
    } else {//图片选择
      tempFilePath = res.tempFilePaths[0]
      imgArr = res.tempFilePaths
      size = res.tempFiles[0].size
    }


    console.log('imgArr------', size, imgArr, isWxAli)

    //跳转方式
    let goType = 'redirectTo'
    if (goTypeM) {
      goType = goTypeM
    }
    if (size > 1 * 1024 * 1024) {
      wx.showToast({
        title: '图片准备中...',
        icon: 'none',
        duration: 2000
      })
      wx.getImageInfo({
        src: tempFilePath,
        success(image) {
          let desHeight, desWidth;
          if //(image.orientation.indexOf('up') < 0 && image.orientation.indexOf('down') < 0) {
          (image.width < image.height) {
            desWidth = image.width > 1200 ? 1200 : image.width;
            desHeight = desWidth * image.height / image.width;
          } else {
            desHeight = image.height > 1200 ? 1200 : image.height;
            desWidth = desHeight * image.width / image.height;
          }
          console.log('getImageInfo:', image, desWidth, desHeight);
          const ctx = wx.createCanvasContext('mycanvas', that);
          ctx.drawImage(tempFilePath, 0, 0, desWidth, desHeight);
          ctx.draw(false, setTimeout(function () {
            wx.canvasToTempFilePath({
              x: 0,
              y: 0,
              width: desWidth,
              height: desHeight,
              destWidth: desWidth,
              destHeight: desHeight,
              canvasId: 'mycanvas',
              quality: 0.93,
              fileType: 'jpg',
              success: function success(res2) {
                // 上传图片
                wx[goType]({
                  url: `/pages/process/process?tempFilePaths=${res2.tempFilePath}&ocrType=${ocrType}`,
                })
              },
              fail: function fail(e) {
                console.log('canvasToTempFilePath: error', tempFilePath, e)
                wx.showToast({
                  title: e.errMsg,
                  icon: 'none'
                })
              },
              complete: function complete(e) { }
            }, that)
          }, 850));
        }
      })

    } else {
      wx[goType]({
        url: `/pages/process/process?tempFilePaths=${imgArr}&ocrType=${ocrType}`,
      })
    }
  },
  valid: valid,
  globalData:{
    cloudID:'',
    encryptedData:'',
    iv:'',
    userInfo:null,
    token:'',
    openid:'',
    expires_in:0,//过期时间
  },
})
